package com.teng.smartdrawer.fragments;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.teng.smartdrawer.PartActivity;
import com.teng.smartdrawer.R;
import com.teng.smartdrawer.adapter.RecommendAdapter;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.bean.Module;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.bean.Recommend;
import com.teng.smartdrawer.bean.Seek;
import com.teng.smartdrawer.parser.DataConstants;
import com.teng.smartdrawer.parser.DataParser;
import com.teng.smartdrawer.popUtils.PopupList;
import com.teng.smartdrawer.popUtils.PopupListAdapter;
import com.teng.smartdrawer.utils.CustomDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by teng on 16/2/23.
 */
public class SearchResultFragment extends Fragment implements View.OnClickListener {
    ListView listView;
    private RecommendAdapter recommendAdapter;
    private List<Recommend> recommendList;
    private Component component;
    private String option;
    private Dialog progressDialog;
    private int number;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recommendList = new ArrayList<Recommend>();
        progressDialog = CustomDialog.createLoadingDialog(getActivity(), "正在操作...");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_search_result , null);
        listView = (ListView) ret.findViewById(R.id.search_result_listview);
        recommendAdapter = new RecommendAdapter(getContext() , recommendList);
        listView.setAdapter(recommendAdapter);
        recommendAdapter.setOnCountChangeListener(this);
        return ret;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Recommend recommend = recommendList.get(position);

                if (recommend instanceof Module){

                    Intent intent = new Intent(getContext() , PartActivity.class);
                    Module module = (Module) recommend;
                    intent.putExtra("id" , module.getD_id());
                    intent.putExtra("name" , module.getD_name());
                    intent.putExtra("type" , 1);
                    startActivity(intent);

                }

            }
        });


//        List<String> popupMenuItemList = new ArrayList<>();
//        popupMenuItemList.add("转发");
//        popupMenuItemList.add("删除");
//
//        PopupList.getInstance().initPopupList(getContext(), listView, popupMenuItemList, new PopupListAdapter.OnPopupListClickListener() {
//            @Override
//            public void onPopupListItemClick(View contextView, int contextPosition, View view, int position) {
//                Toast.makeText(getContext(), contextPosition + "," + position , Toast.LENGTH_LONG).show();
//            }
//        });

    }

    public void updateData(List<Recommend> recommends){
        Log.e("update" , "update   ------>>   "+recommends.toString());
        recommendList.clear();
        recommendList.addAll(recommends);
        recommendAdapter.notifyDataSetChanged();
    }


    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what){
                case DataConstants.GET_COMPONENT_OPTION_SIGN:

                    int status = (int) msg.obj;

                    if (progressDialog != null && progressDialog.isShowing()){
                        progressDialog.dismiss();
                    }

                    if (status == 200){

                        if (option.equals("put")){
                            int m_number = component.getM_number();
                            component.setM_number(m_number + number);

                        }else if(option.equals("get")){
                            int m_number = component.getM_number();
                            component.setM_number(m_number - number);

                        }

                        recommendAdapter.notifyDataSetChanged();
                    }

                    break;
            }


            return false;
        }
    });



    @Override
    public void onClick(View v) {

        int pos = (int) v.getTag();
        component = (Component) recommendList.get(pos);
        number = component.getCount();
        switch (v.getId()){

            case R.id.reduce:

                component.setCount(component.getCount() - 1);
                recommendAdapter.notifyDataSetChanged();
                break;

            case R.id.add:

                component.setCount(component.getCount() + 1);

                recommendAdapter.notifyDataSetChanged();

                break;

            case R.id.dt_put:

                option = "put";

                if (number == 0){
                    Toast.makeText(getContext() , "数量不能等于0" , Toast.LENGTH_SHORT).show();
                    return;
                }

                progressDialog.show();
                DataParser.componentOperateResult(getContext(), handler, "add", component.getM_id(), component.getCount() + "");
                break;

            case R.id.dt_get:

                option = "get";

                if (number == 0){
                    Toast.makeText(getContext() , "数量不能等于0" , Toast.LENGTH_SHORT).show();
                    return;
                }

                progressDialog.show();
                DataParser.componentOperateResult(getContext(), handler, "reduce", component.getM_id(), component.getCount() + "");
                break;

        }
    }
}
